package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONUtil;
import com.ruoyi.system.api.domain.Student;
import com.ruoyi.system.pojo.domain.ClassManage;
import com.ruoyi.system.mapper.ClassManageMapper;
import com.ruoyi.system.pojo.dto.StudentUpdateStatusDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StudentManageMapper;
import com.ruoyi.system.pojo.domain.StudentManage;
import com.ruoyi.system.service.IStudentManageService;

/**
 * 学生管理Service业务层处理
 * 
 * @author sxb
 * @date 2024-01-22
 */
@Service
public class StudentManageServiceImpl implements IStudentManageService 
{
    @Autowired
    private StudentManageMapper studentManageMapper;

    @Autowired
    private ClassManageMapper classManageMapper;

    /**
     * 查询学生管理
     * 
     * @param id 学生管理主键
     * @return 学生管理
     */
    @Override
    public StudentManage selectStudentManageById(Long id)
    {
        return studentManageMapper.selectStudentManageById(id);
    }

    /**
     * 查询学生管理列表
     * 
     * @param studentManage 学生管理
     * @return 学生管理
     */
    @Override
    public List<StudentManage> selectStudentManageList(StudentManage studentManage)
    {
        return studentManageMapper.selectStudentManageList(studentManage);
    }

    /**
     * 新增学生管理
     * 
     * @param studentManage 学生管理
     * @return 结果
     */
    @Override
    public int insertStudentManage(StudentManage studentManage)
    {
        ClassManage classManage = classManageMapper.selectClassManageById(studentManage.getClassId());
        studentManage.setClassName(classManage.getClassName());
        studentManage.setSchoolPosition(classManage.getSchoolPosition());
        return studentManageMapper.insertStudentManage(studentManage);
    }

    /**
     * 修改学生管理
     * 
     * @param studentManage 学生管理
     * @return 结果
     */
    @Override
    public int updateStudentManage(StudentManage studentManage)
    {
        ClassManage classManage = classManageMapper.selectClassManageById(studentManage.getClassId());
        studentManage.setClassName(classManage.getClassName());
        studentManage.setSchoolPosition(classManage.getSchoolPosition());
        return studentManageMapper.updateStudentManage(studentManage);
    }

    /**
     * 批量删除学生管理
     * 
     * @param ids 需要删除的学生管理主键
     * @return 结果
     */
    @Override
    public int deleteStudentManageByIds(Long[] ids)
    {
        return studentManageMapper.deleteStudentManageByIds(ids);
    }

    /**
     * 删除学生管理信息
     * 
     * @param id 学生管理主键
     * @return 结果
     */
    @Override
    public int deleteStudentManageById(Long id)
    {
        return studentManageMapper.deleteStudentManageById(id);
    }

    @Override
    public List<StudentManage> getAllStudents() {
        return studentManageMapper.getAllStudents();
    }

    @Override
    public int updateStudentStatus(StudentUpdateStatusDto studentUpdateStatusDto) {
        Long[] ids = studentUpdateStatusDto.getIds();
        for (Long id : ids) {
            StudentManage studentManage = studentManageMapper.selectStudentManageById(id);
            studentManage.setStudentStatus(studentUpdateStatusDto.getStudentStatus());
            studentManage.setExaminationStatus(studentUpdateStatusDto.getExaminationStatus());
            studentManage.setInterviewStatus(studentUpdateStatusDto.getInterviewStatus());
            studentManageMapper.updateStudentManage(studentManage);
        }
        return ids.length;
    }


    /**
     * 远程添加学生
     * @param student
     */
    @Override
    public void remoteInsertStudentManage(Student student) {
        StudentManage studentManage = new StudentManage();
        BeanUtil.copyProperties(student,studentManage,true);
        int insertResult = studentManageMapper.insertStudentManage(studentManage);
        Assert.isTrue(insertResult > 0,"远程调用服务增加学员失败");
    }

}
